<%@ Page Language="C#" Debug="true" %>
<%@ Import namespace="System.Net" %>
<%@ Import namespace="System.Collections.Generic" %>
<%@ Import namespace="Utils" %>
<%
string username = Request.Form["username"];
string password = Request.Form["password"];

string token = (string)Session["token"];

if(String.IsNullOrEmpty(token))
{
	token = ServiceUtil.getAccessToken();

	Session["token"] = token;
}

string retStr = "";
string userid = "";

try
{
	retStr = ServiceUtil.addUser(token, username, password);
	userid = ServiceUtil.findUserId(token, username);
	retStr += ServiceUtil.resetUserPassword(token, userid, password);
	retStr += ServiceUtil.setUserRoles(token, userid, "6907bdde-5b4e-46d6-a0da-aea2c4273b8d", "user");
}
catch(Exception ex)
{
	//Response.Write(ex.Message);
	if(ex.Message.ToLower().IndexOf("unauth") >= 0)
	{
		//refresh the access token
		token = ServiceUtil.getAccessToken();

		Session["token"] = token;

		retStr = ServiceUtil.addUser(token, username, password);
	}
}

Response.ContentType = "application/json";
Response.Write("{\"id\":\""+userid+"\",\"token\":\""+token+"\",\"msg\":\""+retStr+"\"}");
%>